import axios from 'axios'
import Message from 'vue-m-message'
import { localCache } from '@/utils/cache'
const request = config => {
  const instance = axios.create({
    baseURL: 'http://175.178.67.233:8888/',
    // baseURL: 'http://localhost:8888/',
    timeout: 5000,
    withCredentials: true,
  })
  // 添加请求拦截器，添加请求头里的token
  instance.interceptors.request.use(config => {
    if (localCache.getCache('user')) {
      const token = localCache.getCache('user').token || null
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  })
  instance.interceptors.response.use(
    res => {
      return res.data
    },
    err => {
      Message.info(err.response.data.msg)
      // 抛出错误 阻止代码往下执行
      return Promise.reject(err)
    }
  )
  return instance(config)
}
export default request
